package explorer
{
	import mx.events.FlexEvent;
	
	import smashedapples.core.Controller;
	import smashedapples.core.View;

	public class ExplorerController extends Controller
	{
		public function ExplorerController(view:View=null)
		{
			super(view);
			this.addListeners( ["save","search","delete"]);
		}
		
		public function onCreationComplete( event:FlexEvent ):void
		{
			this.view.model.db.getTestTable.execute();
		}
		
		public function get view():ExplorerView
		{
			return this.$view as ExplorerView;
		}
		
		public function onSave( event:AppEvent ):void
		{
			view.model.db.save( ExplorerDB.TEST_TABLE, view.frm.getData() );
		}
		
		public function onSearch( event:AppEvent ):void
		{
			view.model.db.getTestTable.execute();
		}
		
		public function onDelete( event:AppEvent ):void
		{	
			if( view.datagrid.selectedItem == null)
				return;
			
			view.model.db.deleteByKeys( ExplorerDB.TEST_TABLE, {"TEST_TABLE_ID":view.datagrid.selectedItem["TEST_TABLE_ID"]} );
			
			this.view.frm.reset();
		}
		
	}
}